<?php


class Workflow_model extends CI_Model{
	public function __construct(){
		$this->load->library('email');
	
	
	}
	// Send email to User
	public function sendEmail($inData){
	 
		
		$config['protocol'] = 'sendmail';
		$config['mailpath'] = '/usr/sbin/sendmail';
		$config['smtp_host'] = 'localhost';
		$config['smtp_port'] = '25';
		$config['charset'] = 'UTF-8'; 
		$config['priority'] = '1'; 
		$config['mailtype'] = 'html';
		$config['wordwrap'] = TRUE;
		
		$this->email->initialize($config);
	
		$this->email->from("admin@qi-scg.com","Administrator");
		$this->email->to($inData["mail_to"]);
		//$this->email->cc('another@another-example.com');
		//$this->email->bcc('them@their-example.com');
	
		$this->email->subject($inData["email_title"]);
		$this->email->message($inData["email_message"]);
	
		$this->email->send();
	
		//echo $this->email->print_debugger();
		return true;
	}
	
	
	// Send SMS to User
	public function sendSMS($inData){
		//$smsUrl="http://www.thaibulksms.com/sms_api.php?username=0818666851&password=854431&msisdn=0818666851&message=ABCDEF";
		$username = "0818666851";   // username 
		$password = "854431";       // password
		$msisdn = $inData["mobile"];   // เบอร์ที่ต้องการส่ง คันด้วยเครื่องหมาย ,
		$message = $inData["email_title"];
		$sender="";
		$ScheduledDelivery="";
		#$msisdn = "0818666851";
		#$message = "I LOVE ¿¿¿¿";
		//$msisdn = $_GET["msisdn"];
		//echo $msisdn ;
		$url = "http://www.thaibulksms.com/sms_api.php";
		//$url = "http:127.0.0.1";
		$data_string = "username=$username&password=$password&msisdn=$msisdn&message=$message&sender=$sender&ScheduledDelivery=$ScheduledDelivery";
		
		$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
		$result = curl_exec ($ch);
		//echo 	$result;
		curl_close ($ch);
		
		return true;
	}
	
	 
	
	
	public function completeFlow( $workflow_id){
		$data = array(
				 
				"t_id"=>$workflow_id,
		);
		$this->db->where($data);
		$updateData=array(
				"status"=>1,
				"endTime"=>date("Y-m-d H:i:s"),
		);
		$this->db->update("workflow",$updateData);

		
		$data = array(
					
				"t_id"=>$workflow_id,
		);
		$updateData=array(
				"status"=>1,
				//"endTime"=>date("Y-m-d H:i:s"),
		);
		$this->db->where($data);
		$this->db->update("transactionData",$updateData);
		echo $this->db->last_query();
	}
	
	
	public function completeInstance($instance_id,$workflow_id){
		$data = array(
						"instance_id"=>$instance_id,
						"workflow_id"=>$workflow_id,
					);
		$this->db->where($data);
		$updateData=array(
						"status"=>1,
						"endTime"=>date("Y-m-d H:i:s"),
						"user_update"=>$this->session->userdata('user_id'),
							);
		$this->db->update("workflow_instance",$updateData);
		echo $this->db->last_query();
	}
	
	public function checkNextStep($t_id){
		$position_id = $this->session->userdata('position_id');
		$department_id = $this->session->userdata('department_id');
		$factory_id = $this->session->userdata('factory_id');
		// ถ้าเป็นผู้ตั้งต้น
		$outData["t_id"]	=$t_id;
		if(($position_id == 1) and  ($department_id ==2) ){ // tester / QA
			$outData["next_position"] 	= 2;  // head engineering
			$outData["next_department"]	=2;   // QA
			$outData["next_factory"]	=1;   // Fctory
			 
			// Start workflow
			$this->startInstance($outData);
			$outData["test1"]=$this->nextInstance($outData);
			
		}elseif(($position_id == 2) and  ($department_id ==2) ){ // engineer / QA
			$outData["next_position"] 	= 3;  // หัวหน้าส่วน
			$outData["next_department"]	=2;   // QA
			$outData["next_factory"]	=1;   // Fctory
			$this->nextInstance($outData);
		}elseif(($position_id == 3) and  ($department_id ==2) ){ // หัวหน้าส่วน / QA
			$outData["next_position"] 	= 4;  // หัวหน้าแผนก
			$outData["next_department"]	=2;   // QA
			$outData["next_factory"]	=1;   // Fctory
			$this->nextInstance($outData);
		}elseif(($position_id == 4) and  ($department_id ==2) ){ // หัวหน้าแผนก / QA
			$outData["next_position"] 	= 1;  // operator Bangkok
			$outData["next_department"]	=2;   // QA
			$this->nextInstance($outData);
		}elseif(($position_id == 1) and  ($department_id ==3) ){ // operator Bangkok / technic
			$outData["next_position"] 	= 2;  // หัวหน้าส่วน Bangkok
			$outData["next_department"]	=2;   // QA
			$outData["next_factory"]	=1;   // Fctory
			$this->nextInstance($outData);
		}elseif(($position_id == 2) and  ($department_id ==3) ){ // operator Bangkok / technic
			$outData["next_position"] 	= 3;  // หัวหน้าแผนก Bangkok
			$outData["next_department"]	=2;   // QA
			$outData["next_factory"]	=1;   // Fctory
			$this->nextInstance($outData);
		}elseif(($position_id == 3) and  ($department_id ==3) ){ // operator Bangkok / technic
			$outData["next_position"] 	= 4;  // หัวหน้าแผนก Bangkok
			$outData["next_department"]	=2;   // QA
			$outData["next_factory"]	=1;   // Fctory
			$this->nextInstance($outData);
		}elseif(($position_id ==4) and  ($department_id ==3) ){ // operator Bangkok / technic
			$outData["next_position"] 	= 1;  // หัวหน้าแผนก Bangkok
			$outData["next_department"]	=3;   // QA
			$outData["next_factory"]	=1;   // Fctory
			$this->nextInstance($outData);
		}
		// next instant
		// $this->nextInstant();
		
		// หา profile แล้วค่อยส่งออกไปยัง $outData;
		return $outData;
	}
	// new Instant
	public function startInstance($data){
		$data_workflow = array(
						"t_id"=>$data["t_id"],
						"user_id"=>$this->session->userdata('user_id'),
						"startTime"=>date("Y-m-d H:i:s"),
					    //"user_update"=>$this->session->userdata('user_id'),
						"status"=>0
					);
		$this->db->insert("workflow",$data_workflow);
		
		$inData = array(
				"workflow_id"=>$data["t_id"],
				//"user_id"=>$this->session->userdata('user_id'),
				"user_id"=>$this->session->userdata('user_id'),
				"position_id"=>$this->session->userdata('position_id'),
				"department_id"=>$this->session->userdata('department_id'),
				"factory_id"=>$this->session->userdata('factory_id'),
				"startTime"=>date("Y-m-d H:i:s"),
				"endTime"=>date("Y-m-d H:i:s"),
				"user_update"=>$this->session->userdata('user_id'),
				"status"=>1
		);
		
		$this->db->insert("workflow_instance",$inData);
		
		
	    return true;
		//return  $this->db->last_query();
	}
	
	public function nextInstance($data){
		$data = array(
				"workflow_id"=>$data["t_id"],
				//"user_id"=>$this->session->userdata('user_id'),
				"user_id"=>"",
				"position_id"=>$data["next_position"],
				"department_id"=>$data["next_department"],
				"factory_id"=>$data["next_factory"],
				"startTime"=>date("Y-m-d H:i:s"),
				"status"=>0
		);
		$this->db->insert("workflow_instance",$data);
		return true;
	}
	
	public function endInstant(){
		return true;
	}
 
	
	public function getWorkTitle($inData){
		$this->db->where("wf_id",$inData);
		$this->db->where("status","0");
		$rs=$this->db->get("workflow_instance");
		$outData = $rs->result();
		return  $outData[0]->wf_title;
	}
	
	public function getWorkFlow(){
		
		// ตรวจสอบ worklist ของ บุคคล หรือ กลุ่ม
	    // $this->db->where_in('position_id', $this->session->userdata('position_id'));
	 
	     $this->db->where("status","0");
		// $this->db->where("user_id",$this->session->userdata('user_id'));
		// $this->db->or_where("position_id",$this->session->userdata('position_id'));
		 $this->db->where_in('position_id', $this->session->userdata('position_id'));
		 $this->db->where("department_id",$this->session->userdata('department_id'));
		 
		 
		// $this->db->where("status","0");
		 $rs=$this->db->get("workflow_instance");
		//  echo $this->db->last_query();
		 $out_data = array(); // กำหนดค่าใหม่ให้กับ array
		 foreach($rs->result_array() as $a ){
		 	$key["instance_id"] = $a["instance_id"];
		 	$key["workflow_id"] = $a["workflow_id"];
			$key["work_title"] = "Approve Master Reel";
			//$key["work_title"] = $this->getWorkTitle($a["wf_id"]);
		 	$key["startTime"] = $a["startTime"];
		 //	if($a["status"] ==0){
		 	$key["status"] = "รอดำเนินงาน";
		 	$key["from_user"] = $this->user_model->getFullName($a["user_id"]);
		 //	}else{
		 //	$key["from_user"] = $this->user_model->getFullName($a["before_user_id"]);
		 	
		 		
		 //	}
		 	$out_data[] = $key;
		 	
		 }
		 return $out_data;
	}
	
	
	public function getWorkflowInstance($wf_id){
		$this->db->where('workflow_id',$wf_id);
		$rs  = $this->db->get("workflow_instance");
 
		$out_data = array(); // กำหนดค่าใหม่ให้กับ array
		foreach($rs->result_array() as $a ){
			$key["startTime"] = $a["startTime"];
			$key["endTime"] = $a["endTime"];
			if($a["status"] ==0 ){
				$key["status"] = "รอดำเนินงาน";
			}else{
				$key["status"] = "เสร็จ";
			}
			$key["user_approve"] = $this->user_model->getFullName($a["user_update"]);
			$out_data[] = $key;
		}
		//
		return $out_data;
	}
	
	 
	
	
} // end Class
 
?>
